# 38. 最多购买宝石数目
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on('line', function(input) {
input.push(line);
})
rl.on('close', () => {
const n = parseInt(input[0]);
const gems = input.slice(1, n+1).map(Number);
const value = parseInt(input[n+1]);
let left = 0;
let right =0;
let sum = 0;
let maxT = 0;
while(right < gems.length) {
sum += gems[right];
while(sum > value) {
sum -= gems[left];
left++;
}
maxT = Math.max(maxT, right - left + 1);
right++;
}
console.log(maxT);
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30